package com.si.util;

import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * SpringBoot上传文件工具类
 *
 * @author LinkinStar
 */
public class FileHandleUtil {

    /**
     * 绝对路径
     **/
    private static String absolutePath = "";

    /**
     * 静态目录
     **/
    private static String staticDir = "static";

    /**
     * 文件存放的目录
     **/
    private static String fileDir = "/upload/";

    /**
     * 上传单个文件
     * 最后文件存放路径为：static/upload/image/test.jpg
     * 文件访问路径为：http://127.0.0.1:8080/upload/image/test.jpg
     * 该方法返回值为：/upload/image/test.jpg
     *
     * @param inputStream 文件流
     * @param path        文件路径，如：image/
     * @param filename    文件名，如：test.jpg
     * @return 成功：上传后的文件访问路径，失败返回：null
     */
    public static String upload(InputStream inputStream, String path, String filename, HttpServletRequest request) {
        String uploadImgPath = request.getRealPath(path);
        System.out.println(uploadImgPath);
        File imgfile = new File(uploadImgPath);
        if (!imgfile.isFile()) {
            imgfile.mkdirs();
        }
        //第一次会创建文件夹
        // createDirIfNotExists();

        String resultPath = uploadImgPath + filename;

        //存文件
        File uploadFile = new File(resultPath);
        try {
            FileUtils.copyInputStreamToFile(inputStream, uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return filename;
    }

    /**
     * 创建文件夹路径
     */
    private static void createDirIfNotExists() {
        if (!absolutePath.isEmpty()) {
            return;
        }

        //获取跟目录
        File file = null;
        try {
            file = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            throw new RuntimeException("获取根目录失败，无法创建上传目录！");
        }
        if (!file.exists()) {
            file = new File("");
        }

        absolutePath = file.getAbsolutePath();

        File upload = new File(absolutePath, staticDir + fileDir);
        if (!upload.exists()) {
            upload.mkdirs();
        }
    }

    /**
     * 删除文件
     *
     * @param path 文件访问的路径upload开始 如： /upload/image/test.jpg
     * @return true 删除成功； false 删除失败
     */
    public static boolean delete(String path) {
        File file = new File(absolutePath, staticDir + path);
        if (file.exists()) {
            return file.delete();
        }
        return false;
    }

    public static String Upset(MultipartFile mf) {
        //获取文件后缀名
        String end = mf.getOriginalFilename();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        //图片名称 采取时间拼接随机数
        String names = df.format(new Date());
        Random r = new Random();
        for (int i = 0; i < 3; i++) {
            names += r.nextInt(10);
        }
        String diskFileName =   "/"+names + end; //目标文件的文件名


        return diskFileName;
    }
}